package com.macro.admin.controller;

import com.macro.admin.pojo.Admin;
import com.macro.admin.pojo.AdminLoginParam;
import com.macro.admin.pojo.RespBean;
import com.macro.admin.service.AdminService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 登录
 *
 * @Author: Xiao Hai
 * @Date: 2021-04-19 15:59:33
 * @since 1.0.0
 */
@Api(tags = "登录相关接口")
@RestController
public class LoginController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "手机号登录返回token")
    @PostMapping("/login/{phone}/{smsCode}")
    public RespBean login(@ApiParam(name = "phone",value = "手机号",required = true)@PathVariable String phone,@ApiParam(name = "smsCode",value = "短信验证码",required = true) @PathVariable String smsCode){
        return adminService.login(null,null,null,phone,smsCode,null);
    }

    @ApiOperation(value = "登录之后返token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),adminLoginParam.getCode(),null,null,request);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (null == principal){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username);
        admin.setPassword(null);
        return admin;
    }



    @ApiOperation(value = "退出登录")
    @PostMapping("/loginout")
    public RespBean loginout(){
        return RespBean.success("注销成功！");
    }
}
